library(tidyverse)
library(nycflights13)

3.8.1

ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + 
  geom_jitter()

ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + 
  geom_jitter(width = 9, height = 0)

ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + 
  geom_count()

ggplot(data = mpg) +
  geom_boxplot(mapping = aes(x = fl, y = cty), position = "dodge")

ggplot(data = mpg) +
  geom_boxplot(mapping = aes(x = fl, y = cty))

3.9.1

ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
  geom_point() + 
  geom_abline() +
  coord_fixed()

ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
  geom_point() + 
  geom_abline() 

4.4

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

filter(mpg, cyl == 8)
filter(diamonds, carat > 3)

5.2.4

filter(flights, arr_delay >= 120)
filter(flights, dest %in% c("IAH","HOU"))
airlines
filter(flights, carrier %in% c("UA","AA","DL"))
filter(flights, month %in% c(7,8,9))
filter(flights, dep_delay <= 0 & arr_delay > 120)
filter(flights, dep_delay >= 60 & arr_delay < dep_delay - 30)
filter(flights, between(dep_time, 0, 600))
filter(flights, is.na(dep_time))

5.4.1

select(flights, dep_time, dep_delay, arr_time, arr_delay)

I had considered using contains() but doing this would end up grabbing sched_dep_time and/or sched_arr_time too. This same problem would arise from grabbing all the columns from dep_time to arr_delay.

We could also use the column numbers.

select(flights, c(4,6,7,9))
select(flights, dep_time, dep_delay, dep_time, dep_time)
vars <- c("year", "month", "day", "dep_delay", "arr_delay")
select(flights, one_of(vars))
select(flights, contains("TIME"))
select(flights, contains("TIME", ignore.case = FALSE))